package testServer;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.KeyStroke;

public class MyServer {
	static Robot robot;
	static PointerInfo point;
	static ServerSocket serverSocket;
	static Socket socket = null;
	static DataInputStream dataInputStream ;
	static DataOutputStream dataOutputStream ;
	public enum Command {
		HELLO, MOUSEUP, MOUSEDOWN, MOUSELEFT, MOUSERIGHT, KEYUP, KEYDOWN, KEYLEFT, KEYRIGHT,MOUSECLICK;
	}

	public static void main(String[] args) throws AWTException {

	
		robot= new Robot();
		int jump = 20;
		try {
			serverSocket = new ServerSocket(8888);
			System.out.println("Listening :8888");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while (true) {
			try {
				socket = serverSocket.accept();
				dataInputStream = new DataInputStream(socket.getInputStream());
				dataOutputStream = new DataOutputStream(
						socket.getOutputStream());
				
//				System.out.println("ip: " + socket.getInetAddress());
//				System.out.println("message: " + dataInputStream.readUTF());
				dataOutputStream.writeUTF("Hello!");
				String clientdata = dataInputStream.readUTF();
				
				Command key = Command.HELLO;
				if (clientdata.toLowerCase().compareTo("hello") == 0) {
					key = Command.HELLO;
				} else if (clientdata.toLowerCase().compareTo("mouseup") == 0) {
					key = Command.MOUSEUP;
				} else if (clientdata.toLowerCase().compareTo("mousedown") == 0) {
					key = Command.MOUSEDOWN;
				} else if (clientdata.toLowerCase().compareTo("mouseleft") == 0) {
					key = Command.MOUSELEFT;
				} else if (clientdata.toLowerCase().compareTo("mouseright") == 0) {
					key = Command.MOUSERIGHT;
				} else if (clientdata.toLowerCase().compareTo("keyup") == 0) {
					key = Command.KEYUP;
				} else if (clientdata.toLowerCase().compareTo("keydown") == 0) {
					key = Command.KEYDOWN;
				} else if (clientdata.toLowerCase().compareTo("keyleft") == 0) {
					key = Command.KEYLEFT;
				} else if (clientdata.toLowerCase().compareTo("keyright") == 0) {
					key = Command.KEYRIGHT;
				}else if (clientdata.toLowerCase().compareTo("mouseclick") == 0) {
					key = Command.MOUSECLICK;
				}

				switch (key) {
				case HELLO:
					
					break;
				case MOUSEUP:
				{
					point = MouseInfo.getPointerInfo();
					Point b = point.getLocation();
					int x = (int) b.getX();
					int y = (int) b.getY();
					
					robot.mouseMove(x, y-jump);
					System.out.println("ip: " + socket.getInetAddress());
				System.out.println("message: " + key);
				}
					break;
				case MOUSEDOWN:
				{
					point = MouseInfo.getPointerInfo();
					Point b = point.getLocation();
					int x = (int) b.getX();
					int y = (int) b.getY();
					
					robot.mouseMove(x, y+jump);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
				}
					break;
				case MOUSELEFT:
				{
					point = MouseInfo.getPointerInfo();
					Point b = point.getLocation();
					int x = (int) b.getX();
					int y = (int) b.getY();
					
					robot.mouseMove(x-jump, y);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
					
				}
					break;
				case MOUSERIGHT:
				{
					point = MouseInfo.getPointerInfo();
					Point b = point.getLocation();
					int x = (int) b.getX();
					int y = (int) b.getY();
					
					robot.mouseMove(x+jump, y);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
					
					
//					robot.mousePress(InputEvent.BUTTON1_MASK);
//					robot.mouseRelease(InputEvent.BUTTON1_MASK);
					
				}
					break;
				case KEYUP:
					robot.keyPress(KeyEvent.VK_UP);
					robot.keyRelease(KeyEvent.VK_UP);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
					break;
				case KEYDOWN:
					robot.keyPress(KeyEvent.VK_DOWN);
					robot.keyRelease(KeyEvent.VK_DOWN);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
					break;
				case KEYLEFT:
					robot.keyPress(KeyEvent.VK_LEFT);
					robot.keyRelease(KeyEvent.VK_LEFT);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
					break;
				case KEYRIGHT:
					robot.keyPress(KeyEvent.VK_RIGHT);
					robot.keyRelease(KeyEvent.VK_RIGHT);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
					break;
				case MOUSECLICK:
					robot.mousePress(InputEvent.BUTTON1_MASK);
					robot.mouseRelease(InputEvent.BUTTON1_MASK);
					System.out.println("ip: " + socket.getInetAddress());
					System.out.println("message: " + key);
					break;
				default:
					break;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			//finally {
//				if (socket != null) {
//					try {
//						socket.close();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//
//				if (dataInputStream != null) {
//					try {
//						dataInputStream.close();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//
//				if (dataOutputStream != null) {
//					try {
//						dataOutputStream.close();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			}
		}
	}
}